SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID(N'MyAggregate') IS NOT NULL
	DROP function MyAggregate;

go

create function MyAggregate(
    @col1 int
)

returns nvarchar(4000)
--with schemabinding
as begin
    declare @ret nvarchar(4000);
    set @ret = N'';
    select
        @ret = @ret + ', #' + col2
    from
        tab1
    where
        col1 = @col1;

	select @ret = substring(@ret, 3, 4000);    

    return @ret;
end
GO

select
    col1,
    dbo.MyAggregate(col1) as col2
from
    tab1
group by
    col1
;